home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18505 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. From: ChrisHines@msn.com (Chris Hines)
  2. Subject: RE: INITIALIZATION of a MEMBER CLASS of a CLASS
  3. Date: 21 Apr 96 02:41:01 -0700
  4. References: <4l37o3$iug@risky.ecs.umass.edu>
  5. Message-ID: <00001a81+0000b279@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c++
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. Kiran Toutireddy writes:
  11.  
  12. <snip>
  13.  
  14. >    class Engine{
  15. >        nuts..bolts..pipes..rods...;
  16. >    public:
  17. >        Engine(){  make this an electric engine,....;}
  18. >        Engine(int Gas){ make it a gas engine..}
  19. >        Engine(float Deisel){ make it a deisel engine;}
  20. >    };
  21. >
  22. >
  23. >    class Car{
  24. >        Engine car_engine;
  25. >    public:
  26. >        Car(){ how do i initialize the car_engine to be of type Gas here?;}
  27. >    };
  28. >
  29. >I tried initializing the car_engine by saying Engine car_engine(int 
  30. Gas), but it >gives a warning saying that ANSI c++ forbids such 
  31. declarations. 
  32. >
  33. >Can anybody suggest a better way of doing this?
  34. >thanks in advance
  35. >-kiran
  36.  
  37. Try this, write the implementation of the constructor for Car as follows:
  38.  
  39. Car::Car() : car_engine(1)
  40. {
  41.     <snip>
  42. }
  43.  
  44. The constructor for car_engine is called prior to the body of the 
  45. constructor for Car.  If you have Stroustrup, look up Member, 
  46. constructor for class in the index.
  47.  
  48. ------
  49. ChrisHines@msn.com
  50. The sooner you start coding, the longer it will take you to finish.
  51.